<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Comet echo RPC</title>

		<script type="text/javascript">
			var djConfig = { isDebug: false };
		</script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/dojo/dojo.js"></script>
        <script type="text/javascript" src="${pageContext.request.contextPath}/js/prototype.js"></script>
        <script type="text/javascript" src="${pageContext.request.contextPath}/js/behaviour.js"></script>
		<script language="JavaScript" type="text/javascript">
        var EvUtil =
        {
          getKeyCode : function(ev)
          {
            var keyc;
            if (window.event)
              keyc=window.event.keyCode;
            else
              keyc=ev.keyCode;
            return keyc;
          }
        };

        var echoBehaviours =
        {
            '#phrase' : function(element)
            {
              element.setAttribute("autocomplete","OFF");
              element.onkeyup = function(ev)
              {
                  var keyc=EvUtil.getKeyCode(ev);
                  if (keyc==13 || keyc==10)
                  {
                    sendChatMessage($('phrase').value);
                    $('phrase').value='';
	                return false;
	              }
                  return true;
              }
            },

            '#sendB' : function(element)
            {
              element.onclick = function(event)
              {
                sendChatMessage($('phrase').value);
                $('phrase').value='';
                return false;
              }
            }
          };

        function sendChatMessage(msg) {
            var m = { 'sender': "SomeUserName", 'text': msg };
            dojox.cometd.publish("/chat", m);
        }

        Behaviour.register(echoBehaviours);

        function handleChatMessage(msg) {
            $("responses").innerHTML+=msg.timestamp+" "+msg.channel+": "+msg.data.sender+": " +msg.data.text+"<br/>";
        }

        dojo.require("dojox.cometd");
        dojo.connect(dojox.cometd, "finishInit",
            function(type, data, evt, request) {
                dojox.cometd.subscribe("/chat", "handleChatMessage");
            }
        );
        dojox.cometd.init("${pageContext.request.contextPath}/cometd");
        </script>
	</head>
	<body>

	<h1>Chat Example</h1>

    <p>This example shows how to create a channel from a Spring context and how to add a Filter to it. In this case the filter obfuscates a list of words if they are entered in the chat.</p>

    <div><input id="phrase" type="text"/> <input id="sendB" class="button" type="submit" name="join" value="Send"/>
    </div>
    <pre id="responses"></pre>

    <h3>All Java Code</h3>

    <pre>
        public class ChatMessage
        {
            public ChatMessage()
            {
            }

            public ChatMessage(String sender, String text)
            {
                this.sender = sender;
                this.text = text;
            }

            private String sender;

            public String getSender() {
                return sender;
            }

            public void setSender(String sender) {
                this.sender = sender;
            }

            private String text;

            public String getText() {
                return text;
            }

            public void setText(String text) {
                this.text = text;
            }

            @Override
            public String toString() {
                return "ChatMessage[sender=" + sender + "; text=" + text + "]";
            }
        }

        public class ChatHtmlFilter implements BayeuxFilter&lt;ChatMessage>
        {
            public ChatMessage filter(BayeuxServer server, BayeuxClient from, ChatMessage message) {
                message.setText(message.getText().replace("&lt;", "&amp;lt;"));
                return message;
            }
        }

        public class ChatFilter implements BayeuxFilter&lt;ChatMessage>
        {
            private Set&lt;String> words = new HashSet&lt;String>();

            @Required
            public void setWords(Set&lt;String> words) {
                this.words = words;
            }

            public ChatMessage filter(BayeuxServer bayeux, BayeuxClient from, ChatMessage o) {
                StringBuilder sb = new StringBuilder(o.getText().length());

                StringTokenizer st = new StringTokenizer(o.getText());
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    if (words.contains(token)) {
                        token = token.replaceAll("[euioa]", "*");
                    }
                    if (sb.length() != 0) {
                        sb.append(" ");
                    }
                    sb.append(token);
                }

                o.setText(sb.toString());

                return o;
            }
        }

        public class ChatBotMessageHandler implements BayeuxMessageHandler&lt;ChatMessage>
        {
            private String name;

            @Required
            public void setName(String name) {
                this.name = name;
            }

            public void handle(BayeuxChannel channel, BayeuxClient sender, ChatMessage message) {
                if (message.getText().compareToIgnoreCase("!time") == 0) {
                    channel.publish(new ChatMessage(name, "The local time is " + new Date()));
                }
            }
        }
    </pre>

    <h3>All Spring Configuration</h3>

    <pre>
        &lt;bean id="chatFilter" class="com.polarrose.spring.bayeux.examples.chat.ChatFilter">
          &lt;property name="words">
            &lt;set>
              &lt;value>microsoft&lt;/value>
              &lt;value>tomcat&lt;/value>
            &lt;/set>
          &lt;/property>
        &lt;/bean>

        &lt;bean id="chatHtmlFilter" class="com.polarrose.spring.bayeux.examples.chat.ChatHtmlFilter"/>

        &lt;bean id="chatChannel" class="com.polarrose.spring.bayeux.BayeuxChannel">
          &lt;property name="bayeuxServer" ref="bayeuxServer"/>
          &lt;property name="name" value="/chat"/>
          &lt;property name="filters">
            &lt;list>
              &lt;ref local="chatHtmlFilter"/>
              &lt;ref local="chatFilter"/>
            &lt;/list>
          &lt;/property>
        &lt;/bean>

        &lt;bean id="chatBotMessageHandlerContainer" class="com.polarrose.spring.bayeux.BayeuxMessageHandlerContainer">
          &lt;property name="bayeuxServer" ref="bayeuxServer"/>
          &lt;property name="channel" ref="chatChannel"/>
          &lt;property name="handler">
            &lt;bean class="com.polarrose.spring.bayeux.examples.chat.ChatBotMessageHandler">
              &lt;property name="name" value="Marvin"/>
            &lt;/bean>
          &lt;/property>
        &lt;/bean>
    </pre>

	</body>
</html>
